var Nav = {
	current:"browse",
	go:function(nav){
		if(Nav.current != nav){
			Nav.clear();
			$('li#nav-'+nav).addClass("selected");
			Nav.current = nav;
		}
	},
	clear:function(){
		$("li.navigation-button.selected").removeClass('selected');
	},
	
	showLoggedin:function(){
		$('ul#navigation-loggedin').removeClass('hidden');
		$('ul#navigation-loggedout').addClass('hidden');
		$('.logged-in').fadeIn();
	},
	showLoggedout:function(){
		$('ul#navigation-loggedin').addClass('hidden');
		$('ul#navigation-loggedout').removeClass('hidden');
		$('.logged-in').fadeOut();
		if(Nav.current == 'funds' || Nav.current == 'payments' || Nav.current == 'profile'){
			Nav.go('browse');
		}
	},
	toggleBanner:function(){
		var $banner = $('#layout-messages');
		var $about = $('li#nav-about');
		var $button = $('.close-button');
		if($banner.is(':visible')){
			$button.fadeOut();
			$banner.slideUp('slow',function(){
				$about.fadeIn();
				Scroll.adjustVisibleScrollArea();
				Scroll.adjustSlimScroll();
			});
		}else{
			$banner.slideDown('slow',function(){
				$about.fadeOut();
				Scroll.adjustVisibleScrollArea();
				Scroll.adjustSlimScroll();
			});
			$button.fadeIn();
		}
	},
	BannerLoading:false,
	BannerOrientationLeft:false,
	loadBanner:function(number){
		var currentBanner = $('.banner:visible');
		var currentNumber = +currentBanner.attr('id').substring(6,7);
		var id = 'dot' + number;
		if(currentNumber != number && Nav.BannerLoading == false){
			Nav.BannerLoading = true;
			$('span.dot').removeClass('selected');
			$('span#'+id).addClass('selected');
			$('#banner'+currentNumber).animate({left:($(window).width()+50)+'px'},1000,function(){
				$(this).hide();
				$(this).css('left','0px');
				if(!Nav.BannerOrientationLeft){
					$('#banner'+number).css('textAlign','right');
				} else {
					$('#banner'+number).css('textAlign','left');
				}
				Nav.BannerOrientationLeft = !Nav.BannerOrientationLeft;
				$('#banner'+number).css('left','-'+($(window).width()+50)+'px');
				$('#banner'+number).show();
				$('#banner'+number).animate({left:'0px'},1000,function(){
					Nav.BannerLoading = false;
				});
			});
		}
	},
	BannerPlay:true,
	playBanner:function(){
		setTimeout(function(){
			if(Nav.BannerPlay){
				var numberOfBanners = $('.banner').length;
				var currentBanner = $('.banner:visible');
				if(currentBanner.length > 0){
					var currentNumber = +currentBanner.attr('id').substring(6,7);
					var nextNumber = currentNumber + 1;
					if(currentNumber >= numberOfBanners){
						nextNumber = 1;
					}
					Nav.loadBanner(nextNumber);
				}
				Nav.playBanner();
			}
		},7500);
	}
};

var Login = {
	loggedin:false,
	login:function(){
		Ajax.run("userService.getSessionUser.test",
			function(data){
				if(data.ID.length == 32){
					$('#profile_name').text(data.NAME);
					$('#payment_total').text(data.PAYMENTS);
					$('#fund_total').text(data.FUNDS);
					$(document).trigger('user:login');
				} else {
					alert('Something went wrong logging you in. Please try again in a few minutes.');
				}
			}
		);
	},
	logout:function(){
		Ajax.run("userService.logout.test",
			function(data){
				if(data == true){
					Nav.showLoggedout();
					Fund.query();
					$(document).trigger('user:logout');
				} else {
					alert('ajax logout failed.');
				}
			}
		);
	},
	attemptLogin:function(){
		if(Valid.validate($('#filterSet_login_form'),true)){
			Ajax.run("userService.login.test",
				function(data){
					if(data == true){
						Login.login();
						$.prettyPhoto.close();
					}else {
						Valid.showMessages($('#pp_full_res'),["Login Failed."]);
					}
				},
				Fundr.getAjaxData($('div#pp_full_res'))
			);
		}
	},
	createUser:function(){
		if(Valid.validate($('#filterSet_signup_form'),true)){
			Ajax.run("userService.createUser.test",
				function(data){
					if(data.length == 32){
						Login.login();
						$.prettyPhoto.close();
					}else {
						Valid.showMessages($('#pp_full_res'),["Signup Failed."]);
					}
				},
				Fundr.getAjaxData($('div#pp_full_res'))
			);
		}
	},
	facebookLogin:function(){
		FB.login(function(response){
			console.log(response);
			if(response.authResponse){
				Ajax.run("userService.facebookConnect.test",
					function(data){
						Login.login();
						Modal.close();
					},
					{uid:response.authResponse.userID,accessToken:response.authResponse.accessToken}
				);
			} else {
				//alert('not ok');
			}
		},{scope:'email'});
	},
	checkSession:function(){
		var minutes = 3;
		var milliseconds = minutes * 60 * 1000;
		milliseconds = milliseconds + 1000; // session is set a 30 minutes, which makes it 10 of these check session calls.  Adding 1 second to make it less likely that the timeout happens right as the session expires.
		setTimeout(function(){
			Ajax.run("userService.getSessionUser.test",
				function(data){
					console.warn('checking session');
					if(data.ID.length == 32){
						Login.checkSession(); // check again in 3 minutes
					} else {
						Login.logout();
					}
				}
			);
		},milliseconds);
	}
};